# coding:utf-8
import numpy as np
from scipy import integrate
import pandas as pd
import pickle
from datetime import datetime

print("first we load data and sort it")
data = pd.read_table("../data/orders1g.tbl", sep='|')

data.sort_values('o_totalprice', inplace=True)
data.reset_index(drop=True, inplace=True)

print("finish data preprocess")

# kde = KernelDensity(kernel='gaussian', bandwidth=2).fit(X)

with open('../models/kde_orders1g_o_totalprice.pkl', 'rb') as f:
    kde = pickle.load(f)


def f_p(*args):
    return np.exp(kde.score_samples(np.array(args[0]).reshape(1, -1))) * (float(args[0]) / float(args[1]))


def f_p2(*args):
    return np.exp(kde.score_samples(np.array(args).reshape(1, -1)))


count = 0
result_list = []
time_list = []

start = datetime.now()

total_min = data['o_totalprice'].min()
total_max = data['o_totalprice'].max()
x_min = total_min
x_max = total_min
ans = float(0)

data = data.values

for row in data:
    pre_x_min = x_min
    pre_x_max = x_max
    x_min = float(row[3]) - float(10000)
    if x_min < total_min:
        x_min = total_min
    x_max = float(row[3]) + float(10000)
    if x_max > total_max:
        x_max = total_max

    current_num_len = len(str(int(row[3])))
    magnification = pow(10, current_num_len)
    if (abs(pre_x_min - x_min) + abs(pre_x_max - x_max)) > (row[3] / float(10)):
        # if pre_x_min != x_min:
        #     a = integrate.quad(f_p, pre_x_min, x_min, epsabs=10.0, epsrel=0.1)[0] * float(1500000)
        # else:
        #     a = float(0)
        # if pre_x_max != x_max:
        #     b = integrate.quad(f_p, pre_x_max, x_max, epsabs=10.0, epsrel=0.1)[0] * float(1500000)
        # else:
        #     b = float(0)
        # ans = ans - a + b
        a = (integrate.quad(f_p, x_min, x_max, epsabs=10.0, epsrel=0.1, args=(magnification,))[0]) * magnification
        b = integrate.quad(f_p2, x_min, x_max, epsabs=10.0, epsrel=0.1)[0]
        if b > 0:
            ans = a / b

    else:
        x_min = pre_x_min
        x_max = pre_x_max

    result_list.append(ans)
    count = count + 1

end = datetime.now()
time_cost = (end - start).total_seconds()
print("time cost {}".format(time_cost))

result_list = np.array(result_list)

print("now we calculate avg relative error")
standard_result = pd.read_csv("../standard_result/row_avg1g.csv")['avg']
standard_result = standard_result.values
relative_error = (abs(standard_result - result_list) / standard_result).mean() * 100

print("relative error is: {}%".format(relative_error))
print("finish data preprocess")
